package com.hrc.springbootdemo.lock;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicReference;
import java.util.concurrent.locks.LockSupport;
import java.util.concurrent.locks.ReentrantLock;

public class MyLockSupport {

    private   int num = 0;

    private  ReentrantLock lock = new ReentrantLock();
    public static void main(String[] args) throws InterruptedException {
        MyLockSupport myLockSupport = new MyLockSupport();
        myLockSupport.wrong();
        Thread.sleep(200 );
        System.out.println(myLockSupport.num);
        abc();
        // abc1();
    }

    /** 并发为问题*/
    public  void wrong(){

        for (int i = 0; i < 100; i++) {
            new Thread(() -> {
                lock.lock();
                try {
                    Thread.sleep(1);
                    num++;
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                } finally {
                    lock.unlock();
                }
            }).start();
        }
    }

    /** 串行打印abc*/
    public static void abc() {
        Thread a = null;
        AtomicReference<Thread> atomicReference = new AtomicReference<>(a);
        Thread c = new Thread(() ->{
            while (true) {
                LockSupport.park();
                System.out.println("c");
                LockSupport.unpark(atomicReference.get());
            }
        });
        Thread b = new Thread(() -> {
            while (true) {
                LockSupport.park();
                System.out.println("b");
                LockSupport.unpark(c);
            }
        });
          a = new Thread(()-> {
              while (true) {
                  System.out.println("a");
                  LockSupport.unpark(b);
                  atomicReference.set(Thread.currentThread());
                  LockSupport.park(Thread.currentThread());
              }

        });

        b.start();
        a.start();
        c.start();
    }

    private static volatile int state =  0;
    private static  int threadNum= 3;
    /** 无锁实现串行打印abc*/
    public static void abc1() {
        ExecutorService threadPool = Executors.newFixedThreadPool(3);

        threadPool.submit(() -> {
            while (true) {
                if (state % threadNum == 1) {
                    System.out.println("b");
                    System.out.println(state);
                    state++;
                }
            }
        });

        threadPool.submit(() -> {
            while (true) {
                if (state % threadNum == 0) {
                    System.out.println("a");
                    System.out.println(state);
                    state++;
                }
            }
        });



        threadPool.submit(() -> {
            while (true) {
                if (state % threadNum == 2) {
                    System.out.println("c");
                    System.out.println(state);
                    state++;
                }
            }
        });
    }
}
